// Daniel Schussler
// project 6 servo

#include <avr/io.h>

int main()
{
	//PORTD pins as input
	DDRD = 0x00;
	//Enable internal pull ups
	PORTD = 0xFF;
	//Set PORTB1 pin as output
	DDRB = 0xFF;
	// TOP = ICR1;
	//output compare OC1A 8 bit non inverted PWM
	//Clear OC1A on Compare Match, set OC1A at TOP
	//Fast PWM
	//ICR1 = 20000 defines 50Hz pwm
	ICR1 = 20000;
	TCCR1A|=(0<<COM1A0)|(1<<COM1A1)|(0<<COM1B0)|(0<<COM1B1)|(0<<FOC1A)|(0<<FOC1B)|(1<<WGM11)|(0<<WGM10); // TCCR1A = 0x82
	TCCR1B|=(0<<ICNC1)|(0<<ICES1)|(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10); //TCCR1B = 0x1A
	//start timer with prescaler 8
	
	
	for(;;)
	{
		if(bit_is_clear(PIND, 0))
		{
			//increase duty cycle
			OCR1A += 10;
			loop_until_bit_is_set (PIND, 0);
		}
		if(bit_is_clear (PIND, 1))
		{
			//decease duty cycle
			OCR1A -= 10;
			loop_until_bit_is_set (PIND, 1);
		}
	}
}

int potPin = 2;    // select the input pin for the potentiometer
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup()
{
	pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop()
{
	val = analogRead(potPin);    // read the value from the sensor
	digitalWrite(ledPin, HIGH);  // turn the ledPin on
	delay(val);                  // stop the program for some time
	digitalWrite(ledPin, LOW);   // turn the ledPin off
	delay(val);                  // stop the program for some time
}
